/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Registration;
import Login.MySqlConnector;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
 *
 * @author Bharath
 */
    public class RegServlet extends HttpServlet {

    private MySqlConnector ms;
    private RegistrationDao rd;
    private RegistrationBean rbean;
    private boolean isMultipart;
    private boolean insert;
    HttpSession hs;

    public RegServlet() {
        ms = new MySqlConnector();
        rd = new RegistrationDao();
        rbean = new RegistrationBean();
                        }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, Exception {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            System.out.println("-------------------------");
            // Check that we have a file upload request
            isMultipart = ServletFileUpload.isMultipartContent(request);
            out.println(isMultipart);
            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);
            // Parse the request
            List /* FileItem */ items = upload.parseRequest(request);
            // Process the uploaded items
            Iterator iter = items.iterator();
            while (iter.hasNext())
            {
                FileItem item = (FileItem) iter.next();

                if (item.isFormField()) {
                    processFormField(item);
                } else {
                    processUploadedFile(item);
                }
            }
            //set sessions
            HttpSession hs = request.getSession(true);
            hs.setAttribute("regmemid", "scf".concat(rd.maxValue()));
            hs.setAttribute("regpassword", "scf".concat(rd.maxValue()));
            rbean.setMemberid("scf".concat(rd.maxValue()));
            rbean.setPassword("scf".concat(rd.maxValue()));
            //calling insert method from registration dao
            insert = rd.insert(rbean);
            // String memid = rbean.getMemberid();
            if (insert == true)
            {
                request.getRequestDispatcher("regsuccess.jsp").forward(request, response);
            }
            else {
                out.print("<script>alert('Registration Failed');location = 'regisration.jsp'</script>");
                //request.getRequestDispatcher("regfailed.jsp").forward(request, response);
            }
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(RegServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(RegServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    public void processFormField(FileItem item) {
        if (item.isFormField())
        {
            String name = item.getFieldName();
            String value = item.getString();
            
            if (name.equals("name")) {
                System.out.println("name="+name);
                System.out.println("Value"+value);
                rbean.setName(value);                
            }
            if (name.equals("surname")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSurname(value);
            }
            if (name.equals("sex")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSex(value);
            }
            if (name.equals("caste"))
            {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setCaste(value);
            }
            if (name.equals("dob"))
            {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setDob(value);
            }
            if (name.equals("age")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setAge(value);
            }
            if (name.equals("qual")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setQualification(value);
            }
            if (name.equals("prof")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setProf(value);
            }
            if (name.equals("org")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setOrg(value);
            }
            if (name.equals("resadd")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setRes_add(value);
            }
            if (name.equals("offadd")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setOff_add(value);
            }
            if (name.equals("state")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setState(value);
            }
            if (name.equals("district")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setDist(value);
            }
            if (name.equals("mandal")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setMandal(value);
            }
            if (name.equals("rphone")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setRes_phone(value);
            }
            if (name.equals("ophone")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setOff_phone(value);
            }
            if (name.equals("mobile")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setMobile(value);
            }
            if (name.equals("email")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setEmail(value);
            }
            if (name.equals("webadd")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setWebadd(value);
            }
            if (name.equals("spouse_name")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSpouse_name(value);
            }
            if (name.equals("ssex")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSsex(value);
            }
            if (name.equals("sdob")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSdob(value);
            }
            if (name.equals("marday")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setMar_aniv(value);
            }
            if (name.equals("squal")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSqual(value);
            }
            if (name.equals("sprof")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSprof(value);
            }
            if (name.equals("sorg")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setSorg(value);
            }
            if (name.equals("c1name")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC1name(value);
            }
            if (name.equals("c1sex")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC1sex(value);
            }
            if (name.equals("c1dob")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC1dob(value);
            }
            if (name.equals("c1qual")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC1qual(value);
            }
            if (name.equals("c2name")) {
                System.out.println("name=c2222222222222"+name);
            System.out.println("Value"+value);
                rbean.setC2name(value);
            }
            if (name.equals("c2sex")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC2sex(value);
            }
            if (name.equals("c2dob")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC2dob(value);
            }
            if (name.equals("c2qual")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC2qual(value);
            }
            if (name.equals("c3name")) {
                System.out.println("name=cc33333333"+name);
            System.out.println("Value"+value);
                rbean.setC3name(value);
            }
            if (name.equals("c3sex")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC3sex(value);
            }
            if (name.equals("c3dob")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC3dob(value);
            }
            if (name.equals("c3qual")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
                rbean.setC3qual(value);
            }
            if (name.equals("reffmemid")) {
                System.out.println("name="+name);
            System.out.println("Value"+value);
            rbean.setReffmemberid(value);
            }
        }
    }

    private void processUploadedFile(FileItem item) {
        if (!item.isFormField()) {
            try {
                System.out.println("---File Item-------->" + item.getSize());
                if (item.getSize() > 0)
                {
                    rbean.setFile(item.getInputStream());
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
